package com.yz.jdd.module.settings;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.IntentHandler;
import com.yz.jdd.module.main.entity.ShopListItem;
import com.yz.jdd.module.settings.model.DeviceEntity;
import com.yz.jdd.module.settings.model.Limitation;
import com.yz.jdd.module.settings.model.VoiceConfig;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_deviceQuery;
import com.yz.jdd.network.request.Request_switchLimitationSetting;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择设备
 * 2020-02-20
 */
public class SelectDeviceActivity extends BaseActivity {

    private String shopId;
    private String shopName;
    /**
     * POS、SMART_BOX、FAST_BOX、MKL
     */
    private String deviceType;
    private Button btn_ok;//确定
    private ImageView back_nav;//
    private TextView title_nav;
    private List<DeviceEntity.DeviceList> deviceLists;//设备数据
    private ArrayList<String> checkedList;
    private RecyclerView recy_device_list;
//    private LinearLayout ll_all_check;//全选布局
    private SelectDeviceAdapter selectDeviceAdapter;
    private ImageView iv_no_data;//无数据图标
//    private CheckBox select_all_check;
    private int limitation;// 是否有限制，1表示有限制，0表示不限制
    private ArrayList<String> limitationList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_device);
        initView();
        deviceQuery();
    }

    private void initView() {
        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doSwitchLimitationSetting();
            }
        });
        recy_device_list = findViewById(R.id.recy_device_list);
        recy_device_list.setLayoutManager(new LinearLayoutManager(this));
        title_nav = findViewById(R.id.title_nav);
        title_nav.setText("选择设备");
        iv_no_data = findViewById(R.id.iv_no_data);
        back_nav = findViewById(R.id.back_nav);
        back_nav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        select_all_check = findViewById(R.id.select_all_check);
//        ll_all_check = findViewById(R.id.ll_all_check);

        Intent intent = getIntent();
        shopId = intent.getStringExtra("shopId");
        deviceType = intent.getStringExtra("deviceType");
        limitation = intent.getIntExtra("limitation", 0);
        ArrayList<Limitation> arrayList = (ArrayList<Limitation>) intent.getSerializableExtra("limitationList");
        if (arrayList != null && arrayList.size() > 0) {
            limitationList = new ArrayList<>();
            for (Limitation limitation : arrayList) {
                limitationList.add(limitation.getQrCode());
            }
        }
    }

    /**
     * 获取设备列表信息
     */
    private void deviceQuery() {
        if (!TextUtils.isEmpty(shopId)) {
            showLoading("加载中，请稍后...");
            final Request_deviceQuery request_deviceQuery = new Request_deviceQuery();
            request_deviceQuery.deviceType = deviceType;
            request_deviceQuery.shopIdList = new String[]{shopId};
            NetWorkRequset.deviceQuery(request_deviceQuery, new MySubscriber() {
                @Override
                public void onSuccess(Response_Base response_baseModel) {
                    dismissLoading();
                    deviceLists = new ArrayList<>();
                    if (response_baseModel.getCode().equals("1")) {
                        List<DeviceEntity> list = (ArrayList<DeviceEntity>) response_baseModel.getResultObject();
                        intData(list);
                    }
                }

                @Override
                public void onNeedLogin(String msg) {
                    dismissLoading();
                    IntentHandler.startLoginActivity(SelectDeviceActivity.this);
                }

                @Override
                public void onFail(Response_Base response_baseModel) {
                    dismissLoading();
                    if (!TextUtils.isEmpty(response_baseModel.getMsg())) {
                        Toast.makeText(SelectDeviceActivity.this, response_baseModel.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            Toast.makeText(SelectDeviceActivity.this, "缺少参数", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 加载数据
     *
     * @param list
     */
    private void intData(List<DeviceEntity> list) {
        if (list != null && list.size() > 0) {
            if (list.size() == 1 && list.get(0).deviceList.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    DeviceEntity.DeviceList devices = new DeviceEntity.DeviceList();
                    devices.ShopNameLin = list.get(i).shopName;
                    shopName = devices.ShopNameLin;

                    for (DeviceEntity.DeviceList deviceEntity : list.get(i).deviceList) {
                        if (limitation == 0) {
//                            deviceEntity.selected = true;
                        } else {
                            if (limitationList != null && limitationList.size() > 0) {
                                for (String sn : limitationList) {
                                    if (sn.equals(deviceEntity.machineToolSn)) {
                                        deviceEntity.selected = true;
                                    }
                                }
                            }
                        }
                    }
                    deviceLists.add(devices);
                    deviceLists.addAll(list.get(i).deviceList);
                }
                selectDeviceAdapter = new SelectDeviceAdapter(SelectDeviceActivity.this, deviceLists);
                recy_device_list.setAdapter(selectDeviceAdapter);
//                select_all_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                        for (int i = 0; i < deviceLists.size(); i++) {
//                            if (TextUtils.isEmpty(deviceLists.get(i).ShopNameLin)) {
//                                deviceLists.get(i).selected = isChecked;
//                            }
//                        }
//                        selectDeviceAdapter.notifyDataSetChanged();
//                        setSelectSize(deviceLists);
//                    }
//                });
                if (limitation == 0) {
//                    select_all_check.setChecked(true);
//                    for (int i = 0; i < deviceLists.size(); i++) {
//                        if (TextUtils.isEmpty(deviceLists.get(i).ShopNameLin)) {
//                            deviceLists.get(i).selected = true;
//                        }
//                    }
//                    setSelectSize(deviceLists);
                } else {
                    if (limitationList != null && limitationList.size() > 0) {
                        checkedList = new ArrayList<>();
                        checkedList.addAll(limitationList);
                        btn_ok.setText("确定(" + checkedList.size() + ")");
                    }
                }
            } else {
                iv_no_data.setVisibility(View.VISIBLE);
                recy_device_list.setVisibility(View.GONE);
                btn_ok.setVisibility(View.GONE);
//                ll_all_check.setVisibility(View.GONE);
            }
        } else {
            iv_no_data.setVisibility(View.VISIBLE);
            recy_device_list.setVisibility(View.GONE);
            btn_ok.setVisibility(View.GONE);
//            ll_all_check.setVisibility(View.GONE);
        }
    }


    /**
     * 设置设备参数
     */
    private void doSwitchLimitationSetting() {
        showLoading("加载中，请稍后...");
        final Request_switchLimitationSetting request_deviceQuery = new Request_switchLimitationSetting();
        if (VoiceConfig.DeviceType.POS.equals(deviceType)) {
            request_deviceQuery.setSwitchType(VoiceConfig.VoiceType.POS_COLLECTION_VOICE);
        } else if (VoiceConfig.DeviceType.SMART_BOX.equals(deviceType)) {
            request_deviceQuery.setSwitchType(VoiceConfig.VoiceType.SMART_BOX_COLLECTION_VOICE);
        } else if (VoiceConfig.DeviceType.FAST_BOX.equals(deviceType)) {
            request_deviceQuery.setSwitchType(VoiceConfig.VoiceType.BOX_COLLECTION_VOICE);
        } else if (VoiceConfig.DeviceType.MKL.equals(deviceType)) {
            request_deviceQuery.setSwitchType(VoiceConfig.VoiceType.MKL_COLLECTION_VOICE);
        }

        request_deviceQuery.setCodeList(checkedList);
        NetWorkRequset.doSwitchLimitationSetting(request_deviceQuery, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                dismissLoading();
                if (response_baseModel.getCode().equals("1")) {
                    resultFilterAct();
                }
            }

            @Override
            public void onNeedLogin(String msg) {
                dismissLoading();
                IntentHandler.startLoginActivity(SelectDeviceActivity.this);
            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                dismissLoading();
                if (!TextUtils.isEmpty(response_baseModel.getMsg())) {
                    Toast.makeText(SelectDeviceActivity.this, response_baseModel.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    public void setSelectSize(List<DeviceEntity.DeviceList> payQrCodes) {
        checkedList = new ArrayList<>();
        if (payQrCodes != null) {
            for (DeviceEntity.DeviceList payQrCode : payQrCodes) {
                if (payQrCode.selected) {
                    checkedList.add(payQrCode.machineToolSn);
                }
            }
        }
        if (checkedList.size() > 0) {
            btn_ok.setText("确定(" + checkedList.size() + ")");
        } else {
            btn_ok.setText("确定");
        }

    }

    /**
     * 确认返回结果
     */
    private void resultFilterAct() {
        Intent intent = new Intent();
        ShopListItem.ResultObjectBean.PageDataBean.ShopsBean shopsBean = new ShopListItem.ResultObjectBean.PageDataBean.ShopsBean();
        shopsBean.setShopId(shopId);
        shopsBean.setShopName(shopName);
        Bundle bundle = new Bundle();
        intent.putExtra("shopsBean", shopsBean);
        intent.putExtras(bundle);
//        intent.putStringArrayListExtra("deviceList", checkedList);
        setResult(Constants.SelectCodeResultCode, intent);
        finish();
    }
}
